package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.oms.common.constant.MessageQueueTagConsts;
import com.zmn.oms.common.dto.OmsRefundRequestDTO;
import com.zmn.oms.manager.utils.OmsMqTagConsts;
import com.zmn.oms.model.dto.order.pay.RefundResultDTO;
import com.zmn.oms.zmn.normal.business.interfaces.refund.RefundBService;
import com.zmn.pay.common.constant.PayConsts;
import com.zmn.pay.gateway.common.model.dro.RefundResultDRO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 类描述：工单退款监听
 *
 * @author heciqi
 * @date 2018/11/30 16:53
 */
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
@Slf4j
public class OrderRefundListener {
	@Autowired
	private RefundBService refundBService;

	/**
	 * 取消退款结果（兼容-上线运行一段时间后删除）
	 * @param message
	 */
	@AliyunMQHandler(tags = OmsMqTagConsts.ZMN_TOPIC_OMS_REFUND_TAG)
	public void refundResult(String message) {
		log.info("工单退款结果消费[{}]", message);
		// 退款结果
		RefundResultDRO refundResultDRO = JSON.parseObject(message, RefundResultDRO.class);
		refundBService.updateRefundResult(refundResultDRO);
	}

	/**
	 * 退款结果通知
	 *
	 * @param message
	 */
	@AliyunMQHandler(tags = MessageQueueTagConsts.OMS_ORDER_REFUND)
	public void refund(String message) {
		log.info("退款结果通知[{}]", message);
		// 退款结果
		RefundResultDRO refundResultDRO = JSON.parseObject(message, RefundResultDRO.class);
		if (NumberUtil.isNullOrZero(refundResultDRO.getChannelId())
				&& Objects.equals(refundResultDRO.getOffLineRefund(), GlobalConsts.YES)) {
			log.info("支付渠道为空，线下支付设置为9");
			refundResultDRO.setChannelId(PayConsts.PAY_CHANNEL_PLATFORM_OFFLINE);
			refundResultDRO.setPayType(PayConsts.PAY_TYPE_MANUAL);
		}
		refundBService.updateNewRefundResult(refundResultDRO);
	}

	/**
	 * 退款结果通知
	 *
	 * @param message
	 */
	@AliyunMQHandler(tags = MessageQueueTagConsts.OMS_ORDER_REFUND_TIMES_CARD)
	public void refundTimesCardOrder(String message) {
		log.info("次卡退款结果通知[{}]", message);
		// 退款结果
		RefundResultDRO refundResultDRO = JSON.parseObject(message, RefundResultDRO.class);
		if (NumberUtil.isNullOrZero(refundResultDRO.getChannelId())
				&& Objects.equals(refundResultDRO.getOffLineRefund(), GlobalConsts.YES)) {
			log.info("支付渠道为空，线下支付设置为9");
			refundResultDRO.setChannelId(PayConsts.PAY_CHANNEL_PLATFORM_OFFLINE);
			refundResultDRO.setPayType(PayConsts.PAY_TYPE_MANUAL);
		}
		refundBService.updateTimesCardWorkRefundResult(refundResultDRO);
	}

	/**
	 * 退款结果通知
	 *
	 * @param message
	 */
	@AliyunMQHandler(tags = MessageQueueTagConsts.OMS_ORDER_DUPLICATE_PAY_REFUND)
	public void duplicatePayRefund(String message) {
		log.info("重复支付退款结果通知[{}]", message);
		// 退款结果
		RefundResultDTO dto = JSON.parseObject(message, RefundResultDTO.class);
		if (NumberUtil.isNullOrZero(dto.getChannelId())
				&& Objects.equals(dto.getOffLineRefund(), GlobalConsts.YES)) {
			log.info("支付渠道为空，线下支付设置为9");
			dto.setChannelId(PayConsts.PAY_CHANNEL_PLATFORM_OFFLINE);
			dto.setPayType(PayConsts.PAY_TYPE_MANUAL);
		}
		OmsRefundRequestDTO refundRequestDTO = JSON.parseObject(dto.getRequestData(), OmsRefundRequestDTO.class);
		dto.setRefundRequestDTO(refundRequestDTO);
		dto.setOrderId(refundRequestDTO.getOrderId());
		dto.setWorkId(refundRequestDTO.getWorkId());
		refundBService.duplicatePayRefund(dto);
	}

	/**
	 * 退款结果通知
	 *
	 * @param message
	 */
	@AliyunMQHandler(tags = MessageQueueTagConsts.OMS_ORDER_DIFFERENCE_REFUND)
	public void differenceRefund(String message) {
		log.info("差额退款结果通知[{}]", message);
		// 退款结果
		RefundResultDRO refundResultDRO = JSON.parseObject(message, RefundResultDRO.class);
		if (NumberUtil.isNullOrZero(refundResultDRO.getChannelId())
				&& Objects.equals(refundResultDRO.getOffLineRefund(), GlobalConsts.YES)) {
			log.info("支付渠道为空，线下支付设置为9");
			refundResultDRO.setChannelId(PayConsts.PAY_CHANNEL_PLATFORM_OFFLINE);
			refundResultDRO.setPayType(PayConsts.PAY_TYPE_MANUAL);
		}
		refundBService.differenceRefundCallback(refundResultDRO);
	}
}
